package com.ruoyi.meeting.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.meeting.domain.AjaxTableResult;
import com.ruoyi.meeting.domain.TmdMeetingApplication;
import com.ruoyi.meeting.domain.TmdMeetingManager;
import com.ruoyi.meeting.dto.TmdMeetingDto;
import com.ruoyi.meeting.dto.TmdMeetingPersonDto;
import com.ruoyi.meeting.service.ITmdMeetingApplicationService;
import com.ruoyi.meeting.service.ITmdMeetingManagerService;
import com.ruoyi.meeting.vo.MeetingRecordVo;
import com.ruoyi.meeting.vo.TmdMeetingApplicationVo;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 会议列表Controller
 *
 * @author ruoyi
 * @date 2024-04-22
 */
@Controller
@RequestMapping("/list/application")
public class TmdMeetingApplicationController extends BaseController {
    private String prefix = "list/application";

    @Autowired
    private ITmdMeetingApplicationService tmdMeetingApplicationService;
    @Autowired
    private ISysDeptService deptService;
    @Autowired
    private ITmdMeetingManagerService tmdMeetingManagerService;
    @Autowired
    private ISysUserService userService;

    @RequiresPermissions("list:application:view")
    @GetMapping()
    public String application(Model mmap) {
        List<SysDept> deptList = deptService.selectAllDeptList();
//        AjaxTableResult ajaxTableResult = new AjaxTableResult();
//        ajaxTableResult.setRows(deptList);
//        ajaxTableResult.setTotal(deptList.size());
        mmap.addAttribute("deptList", deptList);

        //mmap.put("ajaxTableResult", ajaxTableResult);
        return prefix + "/application";
    }

    /**
     * 查询会议列表列表
     */
    @RequiresPermissions("list:application:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(TmdMeetingApplication tmdMeetingApplication) {
        startPage();
        List<MeetingRecordVo> list = tmdMeetingApplicationService.selectMeetingRecordList(tmdMeetingApplication);
        return getDataTable(list);
    }

    /**
     * 导出会议列表列表
     */
    @RequiresPermissions("list:application:export")
    @Log(title = "会议列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(TmdMeetingApplication tmdMeetingApplication) {
        List<TmdMeetingApplication> list = tmdMeetingApplicationService.selectTmdMeetingApplicationList(tmdMeetingApplication);
        ExcelUtil<TmdMeetingApplication> util = new ExcelUtil<TmdMeetingApplication>(TmdMeetingApplication.class);
        return util.exportExcel(list, "会议列表数据");
    }

    /**
     * 新增会议列表
     */
    @GetMapping("/add")
    public String add(Model model) {
        //对add页面发起请求后 查询部门表中的所有部门对象
        List<SysDept> deptList = deptService.selectAllDeptList();
        //对add路径发起请求之后,查询会议室管理表 返回会议室对象
        List<TmdMeetingManager> addList = tmdMeetingApplicationService.selectAddList();
        //对add路径发起请求之后,查询用户表 返回用户对象
        List<SysUser> userList = tmdMeetingApplicationService.selectUserList();
//        AjaxTableResult<SysUser> ajaxTableResult = new AjaxTableResult();
//        ajaxTableResult.setRows(userList);
//        ajaxTableResult.setTotal(userList.size());
//        model.addAttribute("ajaxTableResult", ajaxTableResult);
        model.addAttribute("userList", userList);
        model.addAttribute("deptList", deptList);
        model.addAttribute("addList", addList);
        return prefix + "/add";
    }

//    /**
//     * 新增保存会议列表
//     */
//    @RequiresPermissions("list:application:add")
//    @Log(title = "会议列表", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(TmdMeetingApplication tmdMeetingApplication) {
//        return toAjax(tmdMeetingApplicationService.insertTmdMeetingApplication(tmdMeetingApplication));
//    }

    /**
     * 修改会议列表
     */
    @RequiresPermissions("list:application:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap) {
        //Id 是  会议id 通过会议id来查询人 linkmanId 就是userId
        MeetingRecordVo meetingRecordVo = tmdMeetingApplicationService.selectTmdMeetingApplicationVoById(id);

        List<TmdMeetingPersonDto> meetingPersonList = meetingRecordVo.getMeetingPersonList();
        // 参会人id数组
        List<Integer> joinList = new ArrayList<>();
        // 分享人id数组
        List<Integer> shareList = new ArrayList<>();
        for (TmdMeetingPersonDto eve:meetingPersonList){
            if (eve.getMeetingPersonType() == 0){
                joinList.add(eve.getPersonId());
            }else if (eve.getMeetingPersonType()==1){
                shareList.add(eve.getPersonId());
            }
        }
        //将list转为array
        //对meeting的list进行处理
        meetingRecordVo.setJoinStaff(joinList.toArray(new Integer[joinList.size()]));
        meetingRecordVo.setShareStaff(shareList.toArray(new Integer[shareList.size()]));
        mmap.put("tmdMeetingApplication", meetingRecordVo);

        //通过获取的linkmanId 来查询 联系人
        //通过会议的id查到这个联系人id
        Integer userId = tmdMeetingApplicationService.getLinkmanId(id);
        //通过联系人id 获得联系人对象
        SysUser user = tmdMeetingApplicationService.getUser(userId);

        mmap.put("user", user);

        Integer hostId = tmdMeetingApplicationService.getHostId(id);
        SysUser host = tmdMeetingApplicationService.getUser(hostId);
        mmap.put("host", host);
        //NullPointer
        Long addressId = meetingRecordVo.getAddressId();
        //通过会议地址id 查询到会议室的对象
        TmdMeetingManager tmdMeetingManager = tmdMeetingManagerService.selectTmdMeetingManagerById(addressId);

        mmap.put("tmdMeetingManager",tmdMeetingManager);
        //通过deptId 查询部门的对象
        SysDept dept = deptService.selectDeptById(meetingRecordVo.getDeptId());
        mmap.put("dept",dept);
        //通过会议id 查询到与会人员的对象
        List<TmdMeetingPersonDto> meetPerson = tmdMeetingManagerService.selectMeetPersonById(id);
        mmap.put("meetPerson",meetPerson);
        //        查询人员列表
        mmap.put("userList",userService.selectUserList(new SysUser()));

        return prefix + "/edit";
    }

    /**
     * 修改保存会议列表
     */
    @RequiresPermissions("list:application:edit")
    @Log(title = "会议列表", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(TmdMeetingApplication tmdMeetingApplication) {
        return toAjax(tmdMeetingApplicationService.updateTmdMeetingApplication(tmdMeetingApplication));
    }

    /**
     * 删除会议列表
     */
    @RequiresPermissions("list:application:remove")
    @Log(title = "会议列表", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(tmdMeetingApplicationService.deleteTmdMeetingApplicationByIds(ids));
    }

    @GetMapping("/selectUserList")
    @ResponseBody
    public AjaxTableResult<SysUser> selectUserList() {
        List<SysUser> userList = tmdMeetingApplicationService.selectUserList();
        AjaxTableResult<SysUser> ajaxTableResult = new AjaxTableResult();
        ajaxTableResult.setRows(userList);
        ajaxTableResult.setTotal(userList.size());
        return ajaxTableResult;
    }

    @GetMapping("/getUserPhoneNumber")
    @ResponseBody
    public AjaxResult getUserPhoneNumber(Integer userId) {
        SysUser userList = tmdMeetingApplicationService.getUserPhoneNumber(userId);
        if (userList != null) {
            return AjaxResult.success("获取用户信息成功", userList);
        } else {
            return AjaxResult.error("获取用户信息失败");
        }
    }

    @GetMapping("/getDeptList")
    @ResponseBody
    public AjaxTableResult<SysDept> getDeptList() {
        List<SysDept> userList = deptService.selectAllDeptList();
        AjaxTableResult<SysDept> ajaxTableResult = new AjaxTableResult();
        ajaxTableResult.setRows(userList);
        ajaxTableResult.setTotal(userList.size());
        return ajaxTableResult;
    }

    //    获取部门下拉框选中的部门id 进行查询 返回的是AjaxTableResult
    @GetMapping("/getDeptUserList")
    @ResponseBody
    public AjaxTableResult<SysUser> getDeptUserList(Integer deptId) {
        List<SysUser> userList = tmdMeetingApplicationService.getDeptUserList(deptId);
        AjaxTableResult<SysUser> ajaxTableResult = new AjaxTableResult();
        ajaxTableResult.setRows(userList);
        ajaxTableResult.setTotal(userList.size());
        return ajaxTableResult;
    }

    @PostMapping("/apply")
    @ResponseBody
    public AjaxResult apply(TmdMeetingDto tmdMeetingDto) {

        if (tmdMeetingApplicationService.insertTmdMeetingApplication(tmdMeetingDto) > 0) {

            return AjaxResult.success("成功");
        } else {
            return AjaxResult.error("失败");
        }
    }


    /*
     * 查看会议室详情
     * **/
    @GetMapping("/detail/{id}")
    public String detail(@PathVariable("id") Long id, ModelMap mmap) {

        //Id 是  会议id 通过会议id来查询人 linkmanId 就是userId
        MeetingRecordVo meetingRecordVo = tmdMeetingApplicationService.selectTmdMeetingApplicationVoById(id);

        List<TmdMeetingPersonDto> meetingPersonList = meetingRecordVo.getMeetingPersonList();
        // 参会人id数组
        List<Integer> joinList = new ArrayList<>();
        // 分享人id数组
        List<Integer> shareList = new ArrayList<>();
        for (TmdMeetingPersonDto eve:meetingPersonList){
            if (eve.getMeetingPersonType() == 0){
                joinList.add(eve.getPersonId());
            }else if (eve.getMeetingPersonType()==1){
                shareList.add(eve.getPersonId());
            }
        }
        //将list转为array
        //对meeting的list进行处理
        meetingRecordVo.setJoinStaff(joinList.toArray(new Integer[joinList.size()]));
        meetingRecordVo.setShareStaff(shareList.toArray(new Integer[shareList.size()]));
        mmap.put("tmdMeetingApplication", meetingRecordVo);

        //通过获取的linkmanId 来查询 联系人
        //通过会议的id查到这个联系人id
        Integer userId = tmdMeetingApplicationService.getLinkmanId(id);
        //通过联系人id 获得联系人对象
        SysUser user = tmdMeetingApplicationService.getUser(userId);

        mmap.put("user", user);

        Integer hostId = tmdMeetingApplicationService.getHostId(id);
        SysUser host = tmdMeetingApplicationService.getUser(hostId);
        mmap.put("host", host);
        //NullPointer
        Long addressId = meetingRecordVo.getAddressId();
        //通过会议地址id 查询到会议室的对象
        TmdMeetingManager tmdMeetingManager = tmdMeetingManagerService.selectTmdMeetingManagerById(addressId);

        mmap.put("tmdMeetingManager",tmdMeetingManager);
        //通过deptId 查询部门的对象
        SysDept dept = deptService.selectDeptById(meetingRecordVo.getDeptId());
        mmap.put("dept",dept);
        //通过会议id 查询到与会人员的对象
        List<TmdMeetingPersonDto> meetPerson = tmdMeetingManagerService.selectMeetPersonById(id);
        mmap.put("meetPerson",meetPerson);
        //        查询人员列表
        mmap.put("userList",userService.selectUserList(new SysUser()));

        return prefix + "/detail";
    }

    //    会议纪要
    @RequiresPermissions("list:application:edit")
    @GetMapping("/minutes/{id}")
    public String minutes(@PathVariable("id") Long id, ModelMap mmap) {
//        查询当前ID的记录
        TmdMeetingApplication tmdMeetingApplication = tmdMeetingApplicationService.selectTmdMeetingApplicationById(id);
        mmap.put("tmdMeetingApplication", tmdMeetingApplication);
        return prefix + "/record";
    }

    @RequestMapping("/beginning")
    @ResponseBody
    //传入的参数是会议的id
    public AjaxResult beginning(@RequestParam("id") Long id,Model model){
        try{
            //通过会议id 查到这个id对象
            TmdMeetingApplication meetingApplication = tmdMeetingApplicationService.selectTmdMeetingApplicationById(id);
            //获得会议室id
            Long addressId = meetingApplication.getAddressId();
            //通过会议室id来查询会议室对象
            TmdMeetingManager room = tmdMeetingApplicationService.getRoom(addressId);
            //判断是否被会议室是否被占用
            if (room.getStatus() == 0){
                //开始这个会议
                tmdMeetingApplicationService.beginning(id);
                //获取地点id
                int addressIdById = tmdMeetingApplicationService.getAddressIdById(id);
                //通过地点id 来开始会议室
                tmdMeetingApplicationService.beginningRoom(addressIdById);
                return AjaxResult.success("会议开始成功");
            }else{
                return AjaxResult.success("会议室被占用");
            }
        }
        catch (Exception e){
            return AjaxResult.error("会议开始失败" + e.getMessage());
        }
    }

    @RequestMapping("/endless")
    @ResponseBody
    public AjaxResult endless(@RequestParam("id") Long id){
        try{
            //通过会议id 查到这个id对象
            TmdMeetingApplication meetingApplication = tmdMeetingApplicationService.selectTmdMeetingApplicationById(id);
            //获得会议室id
            Long addressId = meetingApplication.getAddressId();
            //通过会议室id来查询会议室对象
            TmdMeetingManager room = tmdMeetingApplicationService.getRoom(addressId);
            //结束会议 改变会议的状态为2
            tmdMeetingApplicationService.endless(id);
            //通过会议室的id 来改变此时会议室的状态
            tmdMeetingApplicationService.endRoom(addressId);
            return AjaxResult.success("会议结束成功");
        }catch (Exception e){
            return AjaxResult.error("会议结束失败" + e.getMessage());
        }
    }

    /**
     *     通过id获取详细信息
     * */
    @RequestMapping("/getMeetingRecord")
    @ResponseBody
    public TmdMeetingApplicationVo getMeetingRecord(@RequestParam("id") Long id,Model model){
        //获取整个会议Vo对象
        TmdMeetingApplicationVo meetingRecord = tmdMeetingApplicationService.getMeetingRecord(id);
        model.addAttribute("meetingRecord",meetingRecord);
        //获取会议的名字
        String meetingRoom = tmdMeetingApplicationService.getMeetingRoom(meetingRecord.getAddressId());
        //通过与会人 id  联系人id 分享人id 来获得人的名字
        //通过会议id 可以查到hostId 和 linkmanId  = > 通过这个可以查到 名字和电话
        //通过会议id在查到 很多组数据在会议人的表中
        return meetingRecord;
    }

    @GetMapping("/getMeetingRoom")
    @ResponseBody
    public AjaxTableResult<TmdMeetingManager> getMeetingRoom() {
        List<TmdMeetingManager> meetingRoom = tmdMeetingManagerService.getMeetingRoom();
        AjaxTableResult<TmdMeetingManager> ajaxTableResult = new AjaxTableResult();
        ajaxTableResult.setRows(meetingRoom);
        ajaxTableResult.setTotal(meetingRoom.size());
        return ajaxTableResult;
    }
}
